package com.saishi.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseOneServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String url = request.getRequestURI();
        int gang = url.lastIndexOf("/");
        String work = url.substring(gang + 1);
        Method m = null;
        try {
            m = this.getClass().getDeclaredMethod(work, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            try {
                m = this.getClass().getDeclaredMethod("doPost", HttpServletRequest.class, HttpServletResponse.class);
                m.invoke(this, request, response);
            } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
				e1.printStackTrace();
//                response.sendError(500, "服务器内部错误！");
            }
//			e.printStackTrace();
//			System.out.println("没有这个" + work);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
